package com.team.teamproject.ui.classfiy;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.team.teamproject.R;
import com.team.teamproject.adapter.ColumsAdapter;
import com.team.teamproject.bean.Category_Colums;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ParseJson;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.team.teamproject.ui.classfiy.MyUrlPath.COLUM_PATH;

/**
 * 分类攻略界面中的栏目
 */

public class StrategyColumFragment1 extends Fragment {
    private static GridView gv;
    private MyHandler handler;
    private static List<Category_Colums> lists;
    private static ColumsAdapter columsAdapter;
    private static ListView lv;
    static class MyHandler extends Handler {
        private WeakReference<Activity> weakReference = null;

        MyHandler(Activity activity) {
            weakReference = new WeakReference<Activity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity activity = weakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        lists = (List<Category_Colums>) msg.obj;
                        //获取栏目集合
                        List<Category_Colums.ColumnsBean> columns = lists.get(0).getColumns();
                        List<Category_Colums.ColumnsBean> columns1 = new ArrayList<>();
                        for (int i = 3; i < 6; i++) {
                            columns1.add(columns.get(i));
                        }
                        columsAdapter = new ColumsAdapter(activity, columns1);
                        // gv.setAdapter(columsAdapter);
                        //设置适配器
                        lv.setAdapter(columsAdapter);
                        break;
                    case 0:
                        Toast.makeText(activity, "下载数据失败", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_category_colum, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        utilGet(COLUM_PATH);
    }

    /**
     * 下载JSON数据
     *
     * @param columPath
     */
    private void utilGet(final String columPath) {
        //开启线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //下载数据
                String json = HttpUtils.getHttpUtils().getJson(columPath);
                if (!TextUtils.isEmpty(json)) {
                    //解析数据
                    List<Category_Colums> categoryColumses = ParseJson.parseJsonToColums(json);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = categoryColumses;
                    handler.sendMessage(msg);
                } else {
                    handler.sendEmptyMessage(0);//下载数据失败
                }
            }
        }).start();
    }

    /**
     * 初始化组件
     *
     * @param view
     */
    private void initView(View view) {
        // gv = (GridView) view.findViewById(R.id.strategy_gv);
        lv = (ListView) view.findViewById(R.id.strategy_lv);
        handler = new MyHandler(getActivity());
    }
}
